﻿using Microsoft.AspNetCore.SignalR.Client;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string token = new Token().get();

            Task.Run(async () =>
            {
                HubConnection connection = new HubConnectionBuilder()
                    .WithUrl("http://45.40.244.50:7000/notify", options =>
                    {
                        options.AccessTokenProvider = () => Task.FromResult($"{token}");
                    })

                    .Build();

                connection.On("Connected", (string message) =>
                {
                    Console.WriteLine("[Connected]" + message);
                });
                connection.On("Sys.User.Active", (string message) =>
                {
                    Console.WriteLine("[User.Active]" + message);
                });
                connection.On("Hicap.QRCodeWarehouse.Service.CodeGenService.ImportFeedback", (string message) =>
                {
                    Console.WriteLine("[Hicap.QRCodeWarehouse.Service.CodeGenService.ImportFeedback]" + message);
                });
                connection.On("Hicap.QRCodeWarehouse.Service.MonitorService.Feedback", (string message) =>
                {
                    Console.WriteLine("[Hicap.QRCodeWarehouse.Service.MonitorService.Feedback]" + message);
                });
                connection.On("Hicap.QRCodeWarehouse.Service.CodeTakenService.Feedback", (string message) =>
                {
                    Console.WriteLine("[Hicap.QRCodeWarehouse.Service.CodeTakenService.Feedback]" + message);
                });
                connection.On("Hicap.QRCodeWarehouse.Service.CodeGenService.Feedback", (string message) =>
                {
                    Console.WriteLine("[Hicap.QRCodeWarehouse.Service.CodeGenService.Feedback]" + message);
                });
                connection.Closed += async (error) =>
                {
                    await Task.Delay(5 * 1000);
                    await connection.StartAsync();
                };
                await connection.StartAsync();
            });

            Console.Read();
        }
    }

    internal class Token
    {
        public string get()
        {
            RestClient client = new RestClient("http://45.40.244.50:5000/connect/token");
            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", "5d303e95-6904-4ec6-a448-94347fd94f99");
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nInitUser\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n123qwe\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_id\"\r\n\r\nHicap.Application\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_secret\"\r\n\r\nHicapApplication_DF64402325E34B398F0A8ED1CCEA65AE\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\npassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            dynamic result = JsonConvert.DeserializeObject(response.Content);

            return result.access_token;
            //            return "eyJhbGciOiJSUzI1NiIsImtpZCI6IjA0MUEyN0VEQzNCRDU4MzNBM0Y5NzQzMTc5NjdFQTg5RDEwMTRBOUQiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJCQm9uN2NPOVdET2otWFF4ZVdmcWlkRUJTcDAifQ.eyJuYmYiOjE1NDIxOTI5NTQsImV4cCI6MTU0MjE5NjU1NCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJIaWNhcC5BcHBsaWNhdGlvbiIsIkhpY2FwLlFSQ29kZVdhcmVob3VzZSJdLCJjbGllbnRfaWQiOiJIaWNhcC5BcHBsaWNhdGlvbiIsInN1YiI6IkluaXRVc2VyIiwiYXV0aF90aW1lIjoxNTQyMTkyOTU0LCJpZHAiOiJsb2NhbCIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzL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